package dao;

import model.Wechat;
import util.ArgsList;
import util.JDBCConnection;
import util.JDBCFactory;
import util.ResultList;

import java.sql.SQLException;
import java.util.List;

public class WechatsManager implements WechatsManagement {
    public static final WechatsManager wechatMan = new WechatsManager();

    @Override
    public String SaveOne(Wechat wechat) {

        if(wechat == null  ||  wechat.getWechat_id() == null  ||  wechat.getU_id() == null) return null;

        String weChatID = wechat.getWechat_id();
        try {
            JDBCConnection conn = JDBCFactory.takeJDBC();
            if (conn.update("insert into t_wechat values(?,?)", wechat.getWechat_id(), wechat.getU_id()) != 1)
                weChatID = null;
            JDBCFactory.returnBack(conn);

            return weChatID;
        } catch(SQLException e) {
            System.err.println(e.getMessage());
            return null;
        }
    }

    @Override
    public String updateOne(long u_id, String wechat_id) {

        String wid = wechat_id;
        try {
            JDBCConnection conn = JDBCFactory.takeJDBC();
            if (conn.update("update t_wechat set wechat_id = ? where u_id = ?", wechat_id, u_id) != 1) wid = null;
            JDBCFactory.returnBack(conn);

            return wid;
        } catch(SQLException e) {
            System.err.println(e.getMessage());
            return null;
        }
    }

    @Override
    public boolean deleteOneByWechatId(String wechat_id) {
        try {
            JDBCConnection conn = JDBCFactory.takeJDBC();
            int count = conn.update("delete from t_wechat where wechat_id = ?", wechat_id);
            JDBCFactory.returnBack(conn);
            return count == 1;
        } catch(SQLException e) {
            System.err.println(e.getMessage());
            return false;
        }
    }

    @Override
    public boolean deleteOneByUserId(long u_id) {
        try {
            JDBCConnection conn = JDBCFactory.takeJDBC();
            int count = conn.update("delete from t_wechat where u_id = ?", u_id);
            JDBCFactory.returnBack(conn);
            return count == 1;
        } catch(SQLException e) {
            System.err.println(e.getMessage());
            return false;
        }
    }

    @Override
    public List<Wechat> query(Wechat wechat, int pageSize, int page) {
        boolean firstCondition = true;

        StringBuilder sql = new StringBuilder("select * from t_wechat");
        ArgsList<Object> arg_whereCondition = new ArgsList<>();

        //构造条件
        if(wechat != null)
        {
            if(wechat.getWechat_id() != null)
            {
                sql.append(" where wechat_id = ?");
                arg_whereCondition.add(wechat.getU_id());
                firstCondition = false;
            }

            if(wechat.getU_id() != null)
            {
                if(!firstCondition) sql.append(" and");
                sql.append(" where u_id = ?");
                arg_whereCondition.add(wechat.getU_id());
            }
        }

        if(pageSize > 0  &&  page > 0)
        {
            sql.append(" limit ?, ?");
            arg_whereCondition.add((Integer)((page - 1) * pageSize));
            arg_whereCondition.add((Integer)pageSize);
        }
        try {
            JDBCConnection conn = JDBCFactory.takeJDBC();
            List<Wechat> result = getResultList(conn.query(arg_whereCondition, sql.toString()));
            JDBCFactory.returnBack(conn);

            return result;
        } catch(SQLException e) {
            System.err.println(e.getMessage());
            return null;
        }
    }

    @Override
    public List<Wechat> query(Wechat wechat) {
        return query(wechat, 0, 0);
    }
    @Override
    public List<Wechat> query(int pageSize, int page) {
        return query(null, pageSize, page);
    }
    @Override
    public List<Wechat> query() {
        return query(null);
    }
    @Override
    public List<Wechat> queryALL() {
        return query();
    }
    @Override
    public List<Wechat> queryALL(int pageSize, int page) {
        return query(pageSize, page);
    }

    /**
     * 包装并返回查询到的对象的列表
     * @param list 包含 SQL 行查询结果的列表，Object[]中每一项都是一列的值
     * @return 返回包装好的对象列表
     */
    private static List<Wechat> getResultList(List<Object[]> list)
    {
        List<Wechat> result = new ResultList<>();
        for(Object[] row : list) result.add(new Wechat(row));
        return result;
    }
}
